import React from 'react'
import { createStore } from '../lib/my-redux'

const reducer = (state = 0, action) => {
    switch(action.type){
        case '+':
            return state + action.num
        case '-':
            return state - action.num
        default:
            return state
    }

}

const store = createStore(reducer)

class Count extends React.PureComponent{


    componentDidMount(){
        store.subscribe(() => this.forceUpdate())
    }

    render(){

        return (
            <div>
                <button onClick={() => store.dispatch({ type: '+', num: 10 })}>+</button>
                <span>{ store.getState() }</span>
                <button onClick={() => store.dispatch({ type: '-', num: 10 })}>-</button>
            </div>
        )
    }
}

export default Count
